/**
 * Created with IntelliJ IDEA.
 * User: tsegelskyi
 * Date: 15.02.13
 * Time: 14:50
 * To change this template use File | Settings | File Templates.
 */
public class UF_QuickFind {
    private int[] id;

    public UF_QuickFind(int N) {
        id = new int[N];
        for (int i = 0; i < N; i++)
            id[i] = i;
    }

    public void union(int p, int q) {
        int pID = id[p];
        int qID = id[q];
        for (int i = 0; i < id.length; i++) {
            if (id[i] == pID) {
                id[i] = qID;
            }
        }
    }

    public boolean connected(int p, int q) {
        return id[p] == id[q];
    }
}
